package org.funismycompta.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.funismycompta.dao.PersonneDao;
import org.funismycompta.modele.Account;
import org.funismycompta.modele.Personne;
import org.funismycompta.testu.DaoTestU;
import org.funismycompta.testu.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;


public class PersonneDaoImpl implements PersonneDao {

	private static final Logger logger = Logger.getLogger(DaoTestU.class);

	public void creerPersonne(Personne p) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction tansaction = null;
		try {
			tansaction = session.beginTransaction();			
			session.save(p);
			// commit
			tansaction.commit();
			logger.debug("COMMIT");
		} catch (RuntimeException e) {
			if (tansaction != null)
				tansaction.rollback();
			logger.debug(e.getMessage() +"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
			throw e;
		} finally {
			logger.debug("CLOSE");
			session.close();
		}
		
	}

	@Override
	public Personne getPersonne(int id) {
		Personne personne = null;
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction tansaction = null;
		try {
			tansaction = session.beginTransaction();
			
			personne = (Personne) session.get(Personne.class, id);
		} catch (RuntimeException e) {
			if (tansaction != null)
				tansaction.rollback();
			logger.debug(e.getMessage() +"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
			throw e;
		} finally {
			logger.debug("CLOSE");
			session.close();
		}
		return personne;
	}

	@Override
	public void supprimerPersonne(Personne p) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			
			session.delete(p);
			transaction.commit();
		} catch (RuntimeException e) {
			if (transaction != null)
				transaction.rollback();
			logger.debug(e.getMessage() +"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
			throw e;
		} finally {
			logger.debug("CLOSE");
			session.close();
		}
		
	}

	@Override
	public void updatePersonne(Personne p) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			
			session.update(p);
			transaction.commit();
		} catch (RuntimeException e) {
			if (transaction != null)
				transaction.rollback();
			logger.debug(e.getMessage() +"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
			throw e;
		} finally {
			logger.debug("CLOSE");
			session.close();
		}		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Account> getListeCompte(Personne p) {
		List<Account> listeComptes = new ArrayList<Account>();
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction transaction = null;
		try {
			listeComptes = session.createQuery(
			"from Account as compte  where compte.personne = ?")
			.setParameter(0, p)
			.list();
		} catch (RuntimeException e) {
			if (transaction != null)
				transaction.rollback();
			logger.debug(e.getMessage() +"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
			throw e;
		} finally {
			logger.debug("CLOSE");
			session.close();
		}
		return listeComptes;
	}

}
